package com.sduept.nwld.dataserver.rest;

import com.sduept.bigdata.fault.common.ETLDAO;
import com.sduept.cim.model.extend.ProtectWaveFile;
import com.sduept.cim.model.extend.WaveFile;
import com.sduept.core.dao.SuperDAO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author     Xiaopy
 * @version    V1.0
 */
@Slf4j
@RestController
@RequestMapping("/waveFileDataSource")
public class FixedWaveDataSource {
	
	
	@Autowired
	private SuperDAO dao;
	@Autowired
	private ETLDAO eDao;
	

	@GetMapping("/fixedData")
	public String fixedData(){
		Set<String> set = getDiffPath();
		updateWaveFile(set);
		updateProtectWaveFile(set);
		log.info("数据更新成功！");
		return "数据更新成功！";
	}


	/**
	 * todo springboot  未测试
	 * @return
	 */
	private Set<String> getDiffPath(){
		Set<String> set = new HashSet<String>();
		String sql= "select distinct mFilePath  from  t_bx_wavefile_list  where mExist='true'";
		List<Object[]> queryList = eDao.query(sql);
		for (Object[] objects : queryList) {
			String mFilePath=String.valueOf(objects[0]);
			set.add(mFilePath);
		}
		return set;
	}
	
	
	private void updateWaveFile(Set<String> paths){
		List<WaveFile> list = dao.findAll(WaveFile.class);
		for(WaveFile file:list){
			if(!paths.contains(file.getPath())){
				file.setDataSource(1);
			}
		}
		try {
			dao.persistentBatchUpdate(WaveFile.class, list);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	private void updateProtectWaveFile(Set<String> paths){
		List<ProtectWaveFile> list = dao.findAll(ProtectWaveFile.class);
		for(ProtectWaveFile file:list){
			if(!paths.contains(file.getPath())){
				file.setDataSource(1);
			}
		}
		try {
			dao.persistentBatchUpdate(ProtectWaveFile.class, list);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
